﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using NHibernate;
using CsharpTrainer.NHibernate3;
using CsharpTrainer.NHibernate3.Mapping;
using CsharpTrainer.NHibernate3.Entities;
using Zeus.Thunder.Core;

namespace SharpTrainer.NHibernate
{
    class UpdateStudent : ITestCase
    {
        public void Run()
        {
            string connStr = ConfigurationManager.ConnectionStrings["NH3TrainConnStr"].ConnectionString;

            try
            {
                NHibernateDb<StudentMap> db = new NHibernateDb<StudentMap>(connStr);

                Console.Write("Please Input Student ID: ");
                int id = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please Input New Student Name: ");
                string name = Console.ReadLine();
                Console.Write("Please Input New Student Age: ");
                int age = Convert.ToInt32(Console.ReadLine());

                ISessionFactory factory = db.SessionFactory;
                using (var session = factory.OpenSession())
                using (var tx = session.BeginTransaction())
                {
                    var student = session.Get<Student>(id);
                    student.StudentName = name;
                    student.Age = age;
                    tx.Commit();
                }

                Console.WriteLine("Student Updated Successfully!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.ToString());
            }
        }
    }
}
